package com.gss.stockLedger.service;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;

import com.gss.stockLedger.domain.FIFOReportDTO;
import com.gss.stockLedger.domain.StockTransactionDtls;
import com.gss.stockLedger.domain.SummaryInv;
import com.gss.stockLedger.domain.SummaryParts;
import com.gss.stockLedger.domain.SummaryReports;
import com.gss.stockLedger.util.ApplicationConstants;

public class ReportSerializerService {
	
	/*static Logger logger = Logger.getLogger("ReportSerializer");
	public static int serializeReport(List<FIFOReportDTO> finalReportList,String reportType,int excelCounter,Workbook workbook)
	{
		
		
		return null;
	}

	
	private static int serializeDetailReport(List<FIFOReportDTO> finalReportList,Date previousTransactDate,int excelCounter,Workbook workbook)
	{
		
		
		FIFOReportDTO fifoReportDTO = null;
		long receiptSummaryQuantity = 0;
		double receiptSummaryValue = 0.0d;
		long consumptionSummaryQuantity = 0;
		double consumptionSummaryValue = 0.0d;
		long closingQuantitySummary = 0;
		double closingValueSummary = 0.0d;
		for(int i=0;i < finalReportList.size();i++){
			fifoReportDTO = finalReportList.get(i);
			if(i==0)
				previousTransactDate = fifoReportDTO.getTransactionDate();
			
			if((i+1) == finalReportList.size())
			{
				fifoReportDTO.printClosingStats = true;
				closingQuantitySummary = fifoReportDTO.getClosingQuantity();
				closingValueSummary = fifoReportDTO.getClosingValue();
			}
			else if(fifoReportDTO.getTransactionDate().compareTo(finalReportList.get(i+1).getTransactionDate()) !=0){
				fifoReportDTO.printClosingStats = true;
			}
			
		
				if(Receipt_Transaction_Type.contains(fifoReportDTO.getTransactionType())){
				if(fifoReportDTO.getQuantity() != null)
					receiptSummaryQuantity+=fifoReportDTO.getQuantity();
				if(fifoReportDTO.getValue() != null)
					receiptSummaryValue+=fifoReportDTO.getValue();
			}else if(Consumption_Transaction_Type.contains(fifoReportDTO.getTransactionType())){
				if(fifoReportDTO.getQuantity() != null)
					consumptionSummaryQuantity+=fifoReportDTO.getQuantity();
				if(fifoReportDTO.getValue() != null)
					consumptionSummaryValue+=fifoReportDTO.getValue();
			}
			if(reportForm.getReportType().equalsIgnoreCase("detailed"))
				excelCreationService.appendCreatedSheet(excelCounter,workbook,fifoReportDTO,i);
			else if(reportForm.getReportType().equalsIgnoreCase("summary_part"))
				createSummaryPartWise(closingQuantitySummary,closingValueSummary,stockTransactionDtls);
			excelCounter++;
			logger.debug("COUNTER: " + excelCounter);
			
			
			
		}
		return excelCounter;
	}
	
	private static int serializeSummaryPart(List<FIFOReportDTO> finalReportList,Date previousTransactDate,int excelCounter)
	{
		SummaryParts summaryParts = new SummaryParts();
		FIFOReportDTO fifoReportDTO = null;
		long closingQuantitySummary = 0;
		double closingValueSummary = 0.0d;
		for (int i=0;i < finalReportList.size();i++) {
			fifoReportDTO = finalReportList.get(i);
			if(i==0)
				previousTransactDate = fifoReportDTO.getTransactionDate();
			
			if((i+1) == finalReportList.size())
			{
				fifoReportDTO.printClosingStats = true;
				closingQuantitySummary = fifoReportDTO.getClosingQuantity();
				closingValueSummary = fifoReportDTO.getClosingValue();
			}
			createSummaryPartWise(fifoReportDTO, summaryParts);
			
		}
		summaryParts.setClosingQuantity(closingQuantitySummary);
		summaryParts.setInventoryType(fifoReportDTO.getInventoryType());
		summaryParts.setPartId(fifoReportDTO.getPartId());
		summaryParts.setPartDesc(fifoReportDTO.getPartDesc());
		summaryParts.setClosingValue(closingValueSummary);
		summaryParts.setClosingRate(closingQuantitySummary>0?closingValueSummary/closingQuantitySummary:0);
		excelCreationService.appendPartSummarySheet(summaryParts);
		
		excelCounter++;
		
		return excelCounter;
	}
	
	private static void createSummaryInvWise(FIFOReportDTO fifoReportDto,SummaryInv summaryInv) {
		
		long summaryQuanitity=0;
		double summaryAmount= 0;
		double summaryRate =0;
		
		if(fifoReportDto!=null&&fifoReportDto.getInventoryType()!=null)
		{
			String invType = fifoReportDto.getInventoryType();
			if(invType.equalsIgnoreCase(ApplicationConstants.TT_INV_AS))
			{
				
			}
		}
	
	}
	*/
	public static void createSummaryPartWise(FIFOReportDTO stockTransactionDtls2,SummaryReports summaryParts) {
		
//		System.out.println("putting "+stockTransactionDtls2.getTransactionType()+":" +summaryParts.getOpeningQuanitity()+":" +stockTransactionDtls2.getQuantity());
		if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_OPENING))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
			{
				summaryParts.setOpeningQuanitity(summaryParts.getOpeningQuanitity()+stockTransactionDtls2.getQuantity());
//				System.out.println("putting opening"+summaryParts.getOpeningQuanitity()+stockTransactionDtls2.getQuantity());
			}
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setOpeningAmount(summaryParts.getOpeningAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setOpeningAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_GRIR))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setzGrirQuantity(summaryParts.getzGrirQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setzGrirAmount(summaryParts.getzGrirAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setzGrirAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_CAPTIVE))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setCaptiveQuantity(summaryParts.getCaptiveQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setCaptiveAmount(summaryParts.getCaptiveAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setCaptiveAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_CONS))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setConsQuantity(summaryParts.getConsQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setConsAmount(summaryParts.getConsAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setConsAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_COSTPRICE))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setCostPriceQuantity(summaryParts.getCostPriceQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setCostPriceAmount(summaryParts.getCostPriceAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setCostPriceAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_CUSTOM))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setCustomQuantity(summaryParts.getCustomQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setCustomAmount(summaryParts.getCustomAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setCustomAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_FIADJ))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setFiAdjQuantity(summaryParts.getFiAdjQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setFiAdjAmount(summaryParts.getFiAdjAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setFiAdjAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_FOC))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setFocQuantity(summaryParts.getFocQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setFocAmount(summaryParts.getFocAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setFocAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_FOREX))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setForexQuantity(summaryParts.getForexQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setForexAmount(summaryParts.getForexAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setForexAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_INVADJ))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setInvAdjQuantity(summaryParts.getInvAdjQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setInvAdjAmount(summaryParts.getInvAdjAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setInvAdjAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_OTHERCONS))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setOtherConsQuantity(summaryParts.getOtherConsQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setOtherConsAmount(summaryParts.getOtherConsAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setOtherConsAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_RETRO))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setRetroQuantity(summaryParts.getRetroQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setRetroAmount(summaryParts.getRetroAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setRetroAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_RTV))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setRtvQuantity(summaryParts.getRtvQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setRtvAmount(summaryParts.getRtvAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setRtvAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_SALES))
		{
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setSalesQuantity(summaryParts.getSalesQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setSalesAmount(summaryParts.getSalesAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setSalesAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
		else if( stockTransactionDtls2.getTransactionType().equalsIgnoreCase(ApplicationConstants.TT_TYPE_SCRAP))
		{
			float rate =0;
			if(stockTransactionDtls2.getQuantity()!=null)
				summaryParts.setScrapQuantity(summaryParts.getScrapQuantity()+stockTransactionDtls2.getQuantity());
			if(stockTransactionDtls2.getValue()!=null)
				summaryParts.setScrapAmount(summaryParts.getScrapAmount()+stockTransactionDtls2.getValue());
			else if(stockTransactionDtls2.getRate()!=null&&stockTransactionDtls2.getRate()!=0&&
					stockTransactionDtls2.getQuantity()!=null&&stockTransactionDtls2.getQuantity()!=0)
			{
				summaryParts.setScrapAmount((double) (stockTransactionDtls2.getQuantity()/stockTransactionDtls2.getRate()));
			}
		}
	
	}

}
